package com.example.myapplication.mroom.database;

import android.content.Context;

import com.example.myapplication.mroom.pojo.StudentRoom;
import com.example.myapplication.mroom.pojo.UserRoom;

import net.sqlcipher.database.SQLiteDatabase;
import net.sqlcipher.database.SupportFactory;

import androidx.annotation.NonNull;
import androidx.room.Database;
import androidx.room.Room;
import androidx.room.RoomDatabase;
import androidx.room.migration.Migration;
import androidx.sqlite.db.SupportSQLiteDatabase;

@Database(entities = {UserRoom.class, StudentRoom.class},version = 2,exportSchema = false)
public abstract class UserRoomDb extends RoomDatabase {


    private static volatile UserRoomDb INSTANCE;

    public abstract UserRoomDao userRoomDao();
    public abstract StudentRoomDao studentRoomDao();

    final static byte[] passphrase = SQLiteDatabase.getBytes("1111".toCharArray());
    final static SupportFactory factory1 = new SupportFactory(passphrase);
    public static UserRoomDb get(Context context){
        if (INSTANCE == null){
            synchronized (UserRoomDb.class){
                if (INSTANCE == null){
                    INSTANCE = Room
                            .databaseBuilder(context.getApplicationContext(),
                                    UserRoomDb.class,
                                    "UserRoomDataBase.db")
//                            .openHelperFactory(factory1)
//                            .addMigrations(MIGRATION1_2,MIGRATION2_3,MIGRATION1_3)
                            .build();
                }
            }
        }
        return INSTANCE;
    }

    static final Migration MIGRATION1_3 = new Migration(1,3) {
        @Override
        public void migrate(@NonNull SupportSQLiteDatabase database) {
            //SQL语句
        }
    };


    static final Migration MIGRATION1_2 = new Migration(1,2){
        @Override
        public void migrate(@NonNull SupportSQLiteDatabase database) {
            database.execSQL("alter table user_room add column age Integer");
        }
    };
    //增加表
   static   final Migration MIGRATION2_3 = new Migration(2,3) {
        @Override
        public void migrate(@NonNull SupportSQLiteDatabase database) {
            database.execSQL("create table user_room(name Text,primary key(id))");

        }
    };




//    public static final UserRoomDb.Companion COMPANION = new UserRoomDb.Companion();
//
//    public static final class Companion{
//
//        public Companion(){}
//
//        public final synchronized UserRoomDb get(final Context context){
//            if (UserRoomDb.instance == null){
//                UserRoomDb.instance = Room.databaseBuilder(context.getApplicationContext(), UserRoomDb.class,"UserRoomDataBase").build();
//            }
//            UserRoomDb userRoomDb = UserRoomDb.instance;
//            Intrinsics.checkNotNull(userRoomDb);
//            return userRoomDb;
//        }
//    }

}
